package com.midea.exam;


import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @description：
 * @author: wangwl23
 * @create: 2021-08-18 16:21
 */
public class RotatePrint {
    static final Object object = new Object();
    static String[] arr = new String[]{"a","b","c","d","e","a","b","c","d","e","a","b","c","d","e"};
    static Integer[] num = new Integer[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    public static Lock lock = new ReentrantLock();
    static Condition condition1 = lock.newCondition();
    static Condition condition2 = lock.newCondition();
    public volatile static boolean flag = false;
    public static void main(String[] args) {
        /*test();
        test2();*/
        test3();
    }
    //synchronized实现线程交替打印
    public static void test(){
        new Thread(() -> {
            synchronized (object) {
                for (String c : arr) {
                    System.out.print(c);
                    try {
                        object.notify();
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
        new Thread(() -> {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (object) {
                for (Integer i : num) {
                    System.out.print(i);
                    try {
                        object.notify();
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }


    //lock实现线程交替打印
    public static void test2(){
        new Thread(() -> {
            Iterator<String> iterator = Arrays.asList(arr).iterator();
            while (iterator.hasNext()){
                try {
                    lock.lock();
                    while(!flag){
                        System.out.print(iterator.next());
                        flag = true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }).start();

        new Thread(() -> {
            Iterator<Integer> iterator = Arrays.asList(num).iterator();
            while (iterator.hasNext()){
                try {
                    lock.lock();
                    while(flag){
                        System.out.print(iterator.next());
                        flag = false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }).start();
    }

    //lock与condition实现线程交替打印
    public static void test3(){
        new Thread(() -> {
            try {
                lock.lock();
                for (int i = 65; i<91;i++){
                    System.out.print((char)i);
                    condition2.signalAll();
                    condition1.await();
                }
                condition2.signalAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();

        new Thread(() -> {
            try {
                lock.lock();
                for (int i = 0; i<26;i++){
                    System.out.print(i);
                    condition1.signalAll();
                    condition2.await();
                }
                condition1.signalAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();
    }

}
